<?php

class MyController extends CController
{
	public $session = NULL;
	public $user = NULL;
	public $website = NULL;
	public $controller_id = NULL;
	
	public $webroot = NULL;

	public $data = NULL;
	public $title = NULL;
	
	public $no_times_visit = NULL;
	public $no_online = NULL;
	
	public $ext = NULL;
	public $theme = NULL;
	
	
	function init()
	{
		$this->webroot = Yii::app()->getRequest()->getHostInfo() .
							Yii::app()->baseUrl;
		
		$this->data = array();
		$this->website = website::model()->findByPk(1);
		$this->title = $this->website->fetitle;
		$this->ext = $this->website->ext;
		
		$theme_default = theme::model()->find(array("condition"=>"`default` = 1"));
		
		if ( ! is_null($theme_default))
		{
			$this->theme = $theme_default->name;
			//echo $this->theme;
			//var_dump(Yii::app()->theme);
		}
		
		$this->controller_id = $this->getId();		
		//echo $this->getRoute();
		
		$count_session_before = session::model()->count();
		
		//session
		$this->session = new CDbHttpSession();
		//ID of the CDbConnection application component: config/main.php
		$this->session->connectionID = "db"; 
		$session_time = $this->website["session_time"];
		$this->session->setTimeout($session_time * 60);
		$this->session->open();

		//$this->session["menuitem_id"] = 0;
		$this->session["menu_alias"] = "";
			
		if ( ! $this->check_user_logged_in())
		{
			$this->session["username"] = "guest";
			$this->session["user_logged_in"] = 0;
			$this->session->close();
			
			// luu thong tin dang nhap vao session hien tai trong db
			$session_current = session::model()->find(array(
				"condition"=>"id = '" . $this->session->getSessionID()."'"
			));
			if (!is_null($session_current))
			{
				$session_current->user_id = 0;
				if (! isset($session_current->time_online) )
					$session_current->time_online = MyHelper::get_date().
						" ".MyHelper::get_time();
				$session_current->ip_online = Yii::app()->getRequest()->getUserHostAddress();
				$session_current->save();
			}
			$this->session->open();
				
		}else{
			// TODO
			$username = $this->session["username"];
			//$language_code = $this->session["language_code"];
			
			$this->user = user::model()->find(array("condition"=>"username = '" .$username. "'"));
		}
		
		$count_session_after = session::model()->count();
		
		if ($count_session_after > $count_session_before)
		{
			$this->website->no_times_visit += ($count_session_after - $count_session_before);
			$this->website->save();
		}
		
		$this->no_times_visit = $this->website->no_times_visit; //luot truy cap
		$this->no_online = $count_session_after; // so nguoi dang online
		
	}
	
	function check_user_logged_in()
	{
		$user_logged_in = $this->session["user_logged_in"];
		return $user_logged_in == 1;
	}
	
	function sqlSelect($strSelect = "", $arrClauses = array())
	{
		
	}
	
	function sqlDelete($table = "", $condition = "")
	{
	}
	
	function sqlUpdate($table = "", $arrClauses = array())
	{
		
	}
}